package com.jinzhi.jzapi.courses.Controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ChapterDO;
import com.jinzhi.jzweb.domain.CoursesDO;
import com.jinzhi.jzweb.pojo.entity.ChapterDocument;
import com.jinzhi.jzweb.pojo.request.ChapterRequest;
import com.jinzhi.jzweb.service.ChapterDocumentService;
import com.jinzhi.jzweb.service.ChapterService;
import com.jinzhi.jzweb.service.CoursesService;
import com.jinzhi.jzweb.service.KnowledgePointsService;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


@Controller
@RequestMapping("/chapter")
@Api(value = "章节管理",tags = "章节管理")
public class AssetsChapterController extends BaseController {

	private final ChapterService chapterService;


	private final CoursesService coursesService;

	private final ChapterDocumentService documentService;

	private final KnowledgePointsService knowledgePointsService;

	public AssetsChapterController(ChapterService chapterService, CoursesService coursesService, ChapterDocumentService documentService, KnowledgePointsService knowledgePointsService) {
		this.chapterService = chapterService;
		this.coursesService = coursesService;
		this.documentService = documentService;
		this.knowledgePointsService = knowledgePointsService;
	}


	@ApiOperation(value = "章节列表页面入口", notes = "章节列表页面入口")
	@GetMapping("/chapterResource")
	String Chapter(Long id){
		return "jzweb/chapterResource/chapterResource";
	}



	@ApiOperation(value = "章节新增页面入口", notes = "章节新增页面入口")
	@GetMapping("/add")
	String add(){
		return "jzweb/chapterResource/add";
	}


	@ApiOperation(value = "章节修改页面入口", notes = "章节修改页面入口")
	@GetMapping("/edit")
	String edit(){
		return "jzweb/chapterResource/edit";
	}



	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		ChapterDO chapter = chapterService.selectById(id);
		model.addAttribute("chapter", chapter);
		CoursesDO coursesDO = coursesService.selectById(chapter.getCoursesId());
		model.addAttribute("coursesName",coursesDO.getName());
		model.addAttribute("proId",coursesDO.getProfileId());
		return "jzweb/chapter/edit";
	}


	@ApiOperation(value = "新增或者修改章节信息接口", notes = "新增或者修改章节信息接口")
	@ResponseBody
	@PostMapping("/saveOrUpdate")
	public Result save(@RequestBody ChapterDO chapter){
		chapter.setCreaterId(this.getUserId());
		return chapterService.insertOrUpdateCharpter(chapter);
	}

	@ApiOperation(value = "新增或者修改章节信息接口", notes = "新增或者修改章节信息接口")
	@ResponseBody
	@PostMapping("/updateCharpter")
	public Result updateCharpter(@RequestBody ChapterDO chapter){
		chapter.setUpdaterId(this.getUserId());
		return chapterService.updateCharpter(chapter);
	}


	@ApiOperation(value = "根据id查询章节信息", notes = "根据id查询章节信息")
	@ResponseBody
	@GetMapping ("/selectById")
	public Result selectById(Long id){
		ChapterDO chapterDO = chapterService.selectById(id);
		chapterDO.setChapterDocuments(documentService.selectList(new EntityWrapper<ChapterDocument>().eq("chapter_id",chapterDO.getId())));
		return Result.ok(chapterDO);
	}


	@ApiOperation(value = "批量删除章节", notes = "批量删除章节")
	@PostMapping( "/batchRemove")
	@ResponseBody
	public Result remove(@RequestBody ChapterDO chapter){
		return chapterService.batchRemoveCharpter(chapter.getChapterDOS());
	}


	@ApiOperation(value = "根据资源id查询章节信息", notes = "根据资源id查询章节信息")
	@PostMapping( "/getChapter")
	@ResponseBody
	public Result getChapter(@RequestBody ChapterRequest request){
		return chapterService.getChapter(request);
	}


	@ApiOperation(value = "效验章节优先级是否重复", notes = "效验章节优先级是否重复")
	@ResponseBody
	@PostMapping("/effect")
	public Result<String> effect(@RequestBody ChapterDO chapterDO){
		ChapterDO coursesDO = chapterService.selectOne(chapterDO);
		if (Objects.isNull(coursesDO)){
			return Result.build(0,"验证成功");
		}else {
			return Result.build(1,"验证失败");
		}
	}

}
